/**  
* @Title: PurchaseController.java  
* @Description: TODO(用一句话描述该文件做什么)  
* @author 张鑫
* @phone 17561783855  
* @date 2019年4月3日  
*/
package com.fortune.rainshine.service.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fortune.rainshine.common.BaseController;
import com.fortune.rainshine.service.service.ProductService;
import com.fortune.rainshine.service.service.PurchaseService;
import com.fortune.rainshinecommon.base.MsgModel;
import com.fortune.rainshinecommon.base.ParmsModel;
import com.fortune.rainshinecommon.enums.ResultCode;
import com.fortune.rainshinecommon.model.ProductModel;
import com.fortune.rainshinecommon.model.service.OrderModel;
import com.fortune.rainshinecommon.model.system.Role;
import com.fortune.rainshinecommon.model.system.User;

/**  
* @ClassName: PurchaseController  
* @Description: TODO(产品采购管理)  
* @author 张鑫
* @phone 17561783855
* @date 2019年4月3日  
*    
*/
@Controller
@RequestMapping("/service/purchase")
public class PurchaseController extends BaseController {
	private final static Logger logger = LoggerFactory.getLogger(ShopController.class);
	@Autowired
	private PurchaseService service;
	@Autowired
	private ProductService productService;

	@RequiresPermissions("purchase:view")
	@RequestMapping()
	public String purchase(Model model, HttpServletRequest request) {
		// 获取商铺id
		String parm3 = request.getHeader("Pass-Check-Session");
    	String parm2 = request.getHeader("Pass-Check-Session").substring(16, parm3.length());
    	String shopId = parm2.substring(0,parm2.length()-9);
 		Role role = this.getLoginUserRole();
 		ParmsModel parm = new ParmsModel(0, 999, null, null);
 		parm.setRoleId(role.getRoleId());
 		List<ProductModel> product = (List<ProductModel>) productService.list(parm).getData();

 		//获取该商铺下的产品库存
		//List<StockModel> product = service.getStockProducts(shopId);
		model.addAttribute("products",product);
		return "purchase/purchaseList.html";
	}
	
	@RequiresPermissions("purchase:order")
    @ResponseBody
    @RequestMapping("/order")
    public MsgModel order(@RequestBody OrderModel model, HttpServletRequest request) {
    	try {
    		// 获取商铺id
    		String parm = request.getHeader("Pass-Check-Session");
        	String parm2 = request.getHeader("Pass-Check-Session").substring(16, parm.length());
        	String shopId = parm2.substring(0,parm2.length()-9);
    		User user = this.getLoginUser();
    		user.setRoleId(this.getLoginUserRole().getRoleId());
    		model.setShopId(Integer.valueOf(shopId));
    		model.setUserId(this.getLoginUser().getUserId());
    		model.setUser(user);
        	return service.order(model);
		} catch (Exception e) {
			logger.error("PurchaseController order =====> {}",e);
			return new MsgModel(ResultCode.FAILURE);
		}
    	
    }
}
